#include "xycall.h"
#include <QVariantMap>
#include <string>
#include <QString>
#include <QDate>
#include "xyaccount.h"

XyCall::XyCall(pj::Account& acc, QObject *parent, int call_id)
    : QObject(parent),pj::Call(acc,call_id),m_xyAccount(acc)
{

}


void XyCall::onCallState(pj::OnCallStateParam &prm)
{
    pj::CallInfo xyInfo = getInfo();
    switch(xyInfo.state)
    {
        case PJSIP_INV_STATE_CALLING:
        {
            QVariantMap xyData;
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
        case PJSIP_INV_STATE_CONNECTING:
        {
            QVariantMap xyData;
            xyData.insert("sipInvate",xyInfo.state);
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
        case PJSIP_INV_STATE_INCOMING:
        {
            QVariantMap xyData;
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
        case PJSIP_INV_STATE_EARLY:
        {
            //自动应答
            /*
            if(PJSIP_ROLE_UAS == xyInfo.role)
            {
                XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
                xyAccount.answer(this);
            }
            */

            QVariantMap xyData;
            if(PJSIP_ROLE_UAC == xyInfo.role)
            {
                xyData.insert("sipRole","UAC");
            }
            else if(PJSIP_ROLE_UAS == xyInfo.role)
            {
                xyData.insert("sipRole","UAS");
            }
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
        case PJSIP_INV_STATE_CONFIRMED:
        {
            QVariantMap xyData;
            if(PJSIP_ROLE_UAC == xyInfo.role)
            {
                xyData.insert("sipRole","UAC");
            }
            else if(PJSIP_ROLE_UAS == xyInfo.role)
            {
                xyData.insert("sipRole","UAS");
            }
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
        case PJSIP_INV_STATE_DISCONNECTED:
        {
            QVariantMap xyData;
            if(PJSIP_ROLE_UAC == xyInfo.role)
            {
                xyData.insert("sipRole","UAC");
            }
            else if(PJSIP_ROLE_UAS == xyInfo.role)
            {
                xyData.insert("sipRole","UAS");
            }
            xyData.insert("sipCode",QString::number(xyInfo.lastStatusCode));
            xyData.insert("sipReason",QString(xyInfo.lastReason.c_str()));
            xyData.insert("sipState",QString::number(xyInfo.state));
            xyData.insert("sipTime",QDate::currentDate().toString("yyyy-MM-dd hh:mm:ss"));
            XyAccount& xyAccount = dynamic_cast<XyAccount&>(m_xyAccount);
            xyAccount.callState(this,xyData);
            break;
        }
    }
}

void XyCall::onCallTsxState(pj::OnCallTsxStateParam &prm)
{
    pj::Call::onCallTsxState(prm);
}

void XyCall::onCallMediaState(pj::OnCallMediaStateParam &prm)
{
    pj::CallInfo xyInfo = getInfo();
    try
    {
        m_xyMicro = pj::Endpoint::instance().audDevManager().getCaptureDevMedia();
        m_xySpeaker = pj::Endpoint::instance().audDevManager().getPlaybackDevMedia();
    }
    catch (pj::Error& err)
    {
        pj::Endpoint::instance().utilLogWrite(1,std::string("call"),"");
        return;
    }
    for(unsigned int i = 0;i < xyInfo.media.size(); ++i)
    {
        if(xyInfo.media[i].type == PJMEDIA_TYPE_AUDIO)
        {
            m_xyRemote = getAudioMedia(i);
            m_xyMicro.startTransmit(m_xyRemote);
            m_xyRemote.startTransmit(m_xySpeaker);
        }
    }
}

void XyCall::onCallSdpCreated(pj::OnCallSdpCreatedParam &prm)
{
    pj::Call::onCallSdpCreated(prm);
}

void XyCall::onStreamCreated(pj::OnStreamCreatedParam &prm)
{
    pj::Call::onStreamCreated(prm);
}

void XyCall::onStreamDestroyed(pj::OnStreamDestroyedParam &prm)
{
    pj::Call::onStreamDestroyed(prm);
}

void XyCall::onDtmfDigit(pj::OnDtmfDigitParam &prm)
{
    pj::Call::onDtmfDigit(prm);
}

void XyCall::onCallTransferRequest(pj::OnCallTransferRequestParam &prm)
{
    pj::Call::onCallTransferRequest(prm);
}

void XyCall::onCallTransferStatus(pj::OnCallTransferStatusParam &prm)
{
     pj::Call::onCallTransferStatus(prm);
}

void XyCall::onCallReplaceRequest(pj::OnCallReplaceRequestParam &prm)
{
    pj::Call::onCallReplaceRequest(prm);
}

void XyCall::onCallReplaced(pj::OnCallReplacedParam &prm)
{
     pj::Call::onCallReplaced(prm);
}

void XyCall::onCallRxOffer(pj::OnCallRxOfferParam &prm)
{
    pj::Call::onCallRxOffer(prm);
}

void XyCall::onCallRxReinvite(pj::OnCallRxReinviteParam &prm)
{
    pj::Call::onCallRxReinvite(prm);
}

void XyCall::onCallTxOffer(pj::OnCallTxOfferParam &prm)
{
    pj::Call::onCallTxOffer(prm);
}

void XyCall::onInstantMessage(pj::OnInstantMessageParam &prm)
{
    pj::Call::onInstantMessage(prm);
}

void XyCall::onInstantMessageStatus(pj::OnInstantMessageStatusParam &prm)
{
    pj::Call::onInstantMessageStatus(prm);
}

void XyCall::onTypingIndication(pj::OnTypingIndicationParam &prm)
{
    pj::Call::onTypingIndication(prm);
}

pjsip_redirect_op XyCall::onCallRedirected(pj::OnCallRedirectedParam &prm)
{
    return pj::Call::onCallRedirected(prm);
}

void XyCall::onCallMediaTransportState(pj::OnCallMediaTransportStateParam &prm)
{
    pj::Call::onCallMediaTransportState(prm);
}

void XyCall::onCallMediaEvent(pj::OnCallMediaEventParam &prm)
{
    pj::Call::onCallMediaEvent(prm);
}

void XyCall::onCreateMediaTransport(pj::OnCreateMediaTransportParam &prm)
{
     pj::Call::onCreateMediaTransport(prm);
}

void XyCall::onCreateMediaTransportSrtp(pj::OnCreateMediaTransportSrtpParam &prm)
{
    pj::Call::onCreateMediaTransportSrtp(prm);
}
